//
//  DailyInspectionController.m
//  PropertyMananger
//
//  Created by bh on 17/1/24.
//  Copyright © 2017年 BH. All rights reserved.
//

#import "DailyInspectionController.h"
#import "DailyInspectionView.h"

@interface DailyInspectionController ()
@property (nonatomic,strong) DailyInspectionView * dailyInspetionView;
@property (nonatomic,strong) NSMutableArray * dailyInspectArr;
@property (nonatomic,strong)  NSDateComponents* comp;//系统的时间
@property (nonatomic,strong) NSString * timeStr;//提交的时间
@end

@implementation DailyInspectionController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initTitleViewName:@"日常巡检"];
    
    self.dailyInspetionView = [[DailyInspectionView alloc]initWithFrame:CGRectMake(0, 0, BHscreenW, BHscreenH)];
    [self.view addSubview:self.dailyInspetionView];
    
    self.dailyInspetionView.recordManLabel.text = userModel.RealName;
    
    [self.dailyInspetionView.confirmButton addTarget:self action:@selector(confirmButton:) forControlEvents:UIControlEventTouchUpInside];
    
    //获取系统时间
    [self getYearData];
    
}
- (void)confirmButton:(UIButton *)button{

    DLog(@"点击提交保存");
    
    [self postDailyInspectionData];
}
#pragma mark - 数据请求
- (void)postDailyInspectionData{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.label.text = @"努力加载数据中...";
    // 隐藏时从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.userInteractionEnabled = YES;
    
    NSDictionary * parms =  @{
                              @"CommunityID":userModel.CommunityID,
                              @"WorkerId":userModel.WorkerID,
                              @"InspectTime": self.dailyInspetionView.recordLabel.text,
                              @"InspectContent":self.dailyInspetionView.textView.text,
                              @"WorkerName":userModel.RealName,
                              @"json":@"\{\"jsonEntity\":\{},\"action\":\"saveinspect\",\"version\":\"V1.0\"}"
                              };
    DLog(@"提交的参数 === %@",parms);
    
    [HomeNetWorkRequest postDailyInspectionSuccess:^(id data) {
        [MBProgressHUD hideHUDForView:self.view];
        
              
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSString * str = [NSString stringWithFormat:@"%@",dict[@"Data"]];
        NSData * data2 = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSArray * array = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
        DLog(@"%@",dict);
        DLog(@"返回 === %@",dict[@"Code"]);
        DLog(@"返回提示 === %@",dict[@"Msg"]);
        DLog(@"日常巡检提交数据 == %@",array);
        
        if (self.dailyInspectArr != 0){
            
            self.placehoderImg.hidden = YES;
            self.titleLabel.hidden = YES;
            
        }else{
            
            self.tableView.mj_footer.hidden = YES;
            
            self.placehoderImg.hidden = NO;
            self.titleLabel.hidden = NO;
        }
        
        if ([[dict[@"Code"]stringValue] isEqualToString:@"1"]) {
            
            [MBProgressHUD showSuccess:dict[@"Msg"] time:1.5];
            
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        [self.tableView reloadData];
    } Fail:^(NSString *errorDes) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        [MBProgressHUD hideHUD];
        
    } param:parms ];
    
}
#pragma mark - 获取系统时间
- (void)getYearData{
    
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSTimeInterval time = [zone secondsFromGMTForDate:date];
    NSDate *dateNow = [date dateByAddingTimeInterval:time];
    NSString * timeDataStr = [NSString stringWithFormat:@"%@",dateNow];
    DLog(@" === %@",dateNow);
    
    NSArray * arr = [timeDataStr componentsSeparatedByString:@"+"];
    DLog(@"日期格式化 == %@",arr[0]);
   
    self.dailyInspetionView.recordLabel.text = [NSString stringWithFormat:@"%@",arr[0]];
    
}

@end
